//
//  EnshrineViewController.m
//  A_Comic
//
//  Created by sos on 16/1/23.
//  Copyright © 2016年 sos. All rights reserved.
//

#import "EnshrineViewController.h"
#import "HomePageModel.h"
#import "MoreTableViewCell.h"
#import <UIImageView+WebCache.h>
#import "DetailsViewController.h"
@interface EnshrineViewController ()<UITableViewDataSource,UITableViewDelegate>
@property (nonatomic,retain) UITableView *tableView;
@property (nonatomic,retain) NSMutableArray *dataArr;
@end

@implementation EnshrineViewController
- (void)dealloc
{
    [_dataArr release];
    [_tableView release];
    [super dealloc];
}
- (void)viewDidLoad {
    [super viewDidLoad];
//    self.navigationController.navigationBarHidden = NO;
    // Do any additional setup after loading the view.
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height+20) style:UITableViewStylePlain];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    
    [self.view addSubview:self.tableView];
    
    [self.tableView registerClass:[MoreTableViewCell class] forCellReuseIdentifier:@"cell"];
    
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
    [self.tableView release];
    
    
    [self data];
    
   
    
}

-(void)data{
    
    
    NSString *path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).lastObject stringByAppendingPathComponent:@"shoucang.plist"];
    
    
    self.dataArr = [NSKeyedUnarchiver unarchiveObjectWithFile:path];
    
    
}

//协议方法
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataArr.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    MoreTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    
    HomePageModel *m = self.dataArr[indexPath.row];
    [cell.img sd_setImageWithURL:[NSURL URLWithString:m.cover_url]];
    cell.nameLabel.text = m.title;
    cell.label.text = [NSString stringWithFormat:@"作者:%@",m.artist_name];
    cell.lastLabel.text = [NSString stringWithFormat:@"更新至%@",m.lastup];
    
    return cell;
    
}
//cell高
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 120;
}

//tableview点击
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    DetailsViewController *deta = [[DetailsViewController alloc]init];
    deta.m = self.dataArr[indexPath.row];
    
    [self.navigationController pushViewController:deta animated:YES];
    
    
}
//tableview编辑
-(NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    
    return @"删除";
}

-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    return UITableViewCellEditingStyleDelete;
    
    
}
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        NSString *path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).lastObject stringByAppendingPathComponent:@"shoucang.plist"];
        
        NSMutableArray *arr = [NSKeyedUnarchiver unarchiveObjectWithFile:path];
        [arr removeObjectAtIndex:indexPath.row];
        [self.dataArr removeAllObjects];
        self.dataArr = arr;
        [NSKeyedArchiver archiveRootObject:self.dataArr toFile:path];
        [self.tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationRight];
        
    }
}

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self.tableView reloadData];
  

    self.navigationController.navigationBarHidden = NO;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
